-- Copyright (C) 2013
--vss.workflow.upload is not OK

-------1定义公共组件(开�?---------------

local cjson = require "cjson"
local storage    = require("resty.gcsdfs.storage")              --加载storage �?
local red_method = require("gcs.list.method")
local utils   = require "gcs.utils"
local iconv = require("iconv")
local mongo_method  = require "gcs.mongodb.method"
local up_utils                 = require "gcs.upload.upload_utils"
local err_utils                = require "gcs.upload.error_utils"
local str_sub  = string.sub
local str_find = string.find
local str_len  = string.len
local tb_concat  = table.concat
local tb_getn = table.getn
local tb_insert = table.insert
local table = table
local type  = type
local pairs = pairs
local ngx = ngx
local os  = os
local string = string
local math_floor = math.floor
local math_random   =math.random
local math_abs    =math.abs
local tostring  = tostring
local tonumber  = tonumber
local cjson = require "cjson"
local vssid = "upload_hkdj"
local model = "upload_hkdj"
local error_table = ngx.shared.gcs_error   --声明ngx.shared.gcs_error
local config_table             = ngx.shared.gcs_config            --定义gcs_config缓存
local ipshine_table = ngx.shared.gcs_ipshine
local resty_uuid               = require "resty.uuid"
local remote_ip                = ngx.var.remote_addr
local cjson_queue              = config_table:get("p_queue")
----------------定义相关的表--------------------------------------
local tb_csys = {}
tb_csys[0]= "未知颜色"
tb_csys[1]= "白色"
tb_csys[2]= "银色"
tb_csys[3]= "灰色"
tb_csys[4]= "黑色"
tb_csys[5]= "红色"
tb_csys[6]= "深蓝"
tb_csys[7]= "蓝色"
tb_csys[8]= "黄色"
tb_csys[9]= "绿色"
tb_csys[10]= "棕色"
tb_csys[11]= "粉色"
tb_csys[12]= "紫色"
local tb_clpp = {}
tb_clpp[0]= "未知品牌"
tb_clpp[1]= "大众"
tb_clpp[2]= "别克"
tb_clpp[3]= "宝马"
tb_clpp[4]= "本田"
tb_clpp[5]= "标致"
tb_clpp[6]= "丰田"
tb_clpp[7]= "福特"
tb_clpp[8]= "日产"
tb_clpp[9]= "奥迪"
tb_clpp[10]= "马自达"
tb_clpp[11]= "雪弗兰"
tb_clpp[12]= "雪铁龙"
tb_clpp[13]= "现代"
tb_clpp[14]= "奇瑞"
local tb_wfxwdm = {}
tb_wfxwdm[0]= "00000" --"正常"
tb_wfxwdm[1]= "43060"--"低速"
--tb_wfxwdm[2]= "超速"
tb_wfxwdm[3]= "13010" --"逆行"
tb_wfxwdm[4]= "16251" --"闯红灯"
tb_wfxwdm[5]= "13451"--"压车道线"
tb_wfxwdm[6]= "12081" --"不按导向"
tb_wfxwdm[7]= "12110" --路口滞留
tb_wfxwdm[8]= "10760" --"机占非"
tb_wfxwdm[9]= "13451" --"违法变道"
tb_wfxwdm[10]= "12081" --"不按车道"
tb_wfxwdm[11]= "13445" --"违法禁令"
tb_wfxwdm[12]= "12110" --"路口停车"
tb_wfxwdm[13]= "12110" --"绿灯停车"

local tb_hpys = {}
tb_hpys[0] = 2 --"蓝色"
tb_hpys[1] = 1 --"黄色"
tb_hpys[2] = 0 --"白色"
tb_hpys[3] = 3 --"黑色"
tb_hpys[4] = 4 --"其它"
----------------------------------------
--
--取时间最后2位 00-59
local sec = os.date("%S",ngx.time())
--取小时前2位 00-23
local hour = os.date("%H",ngx.time())
--取日期后2位00-31
local today = ngx.today()
local day = string.sub(today,-2)
---取月份
local mon = os.date("%Y%m",ngx.time())

--[[local time1=socket.gettime()*1000
utils.gcs_log("error",vssid .. "time1" ..time1)--]]
-------------------函数定义---------------------
function createIconv(from,to,text)        ---编码转换

  local cd = iconv.new(to .. "//TRANSLIT", from)

  local ostr, err = cd:iconv(text)

  if err == iconv.ERROR_INCOMPLETE then
    return nil,"ERROR: Incomplete input."
  elseif err == iconv.ERROR_INVALID then
    return nil,"ERROR: Invalid input."
  elseif err == iconv.ERROR_NO_MEMORY then
    return nil,"ERROR: Failed to allocate memory."
  elseif err == iconv.ERROR_UNKNOWN then
    return nil,"ERROR: There was an unknown error."
  end
  return ostr
end

local tb_queue,err,err_no = up_utils.get_queue(cjson_queue,remote_ip)  ---获取队列信息
local tb_queue = err_utils.check_res(tb_queue,model,vssid,err,err_no,nil)

ngx.req.read_body()

local  data = ngx.req.get_body_data()

if data then 
    data=string.format("%s",data)
    utils.gcs_log("debug","已接受post" .. data)
end

------------------------解析-----------------------
local db_data = cjson.decode(data)
local db_data1 = db_data
db_data1.hphm=createIconv("gbk","utf-8",db_data1.hphm)
db_data1.str_res1=cjson.encode(db_data1.st_res)
db_data1.time1=db_data1.time
db_data1.ip=db_data1.sbip
db_data1.gfile3=db_data1.gfile2
if db_data1.hphm == "" or db_data1.hphm =="无车牌" then
  db_data1.hphm = "无车牌"                         --号牌号码
else
  local tb_reys = {"蓝","黄","白","黑"}
  for _,v in pairs(tb_reys) do
    local i = string.find(db_data1.hphm,v)
    if i then
      db_data1.hphm = string.gsub(db_data1.hphm,v,"",1)
    end
  end
end
if db_data1.hphm == "无车牌" then 
    db_data1.gfile3 = nil
end
db_data1.clpp = tb_clpp[tonumber(db_data1.clpp)] or "未知品牌"
db_data1.csys = tb_csys[tonumber(db_data1.csys)] or "未知颜色"
db_data.wfxwdm = tb_wfxwdm[tonumber(db_data1.wfxwdm)] or "00000" 
db_data1.lx = "dj"
db_data1.hpsztpzb = db_data1.zb
db_data1.zpzs = 1
db_data1.xsfx ="E" --ipshine_table:get("xsfx_" ..db_data1.ip)    --行驶方向
db_data1.sbbh = ipshine_table:get("sbbh_" ..db_data1.ip)  --设备编号
db_data1.lkbh ="0272014LF14001" --ipshine_table:get("lkbh_" ..db_data1.ip)
local tpmc={db_data1.lkbh,db_data1.time1,db_data1.txxh,db_data1.zpzs,
  db_data1.zpxh,db_data1.hphm,db_data1.hpys,db_data1.xssd,db_data1.xsfx,
  db_data1.cd,db_data1.wfxwdm,db_data1.hpsztpzb,db_data1.csys,db_data1.clpp,db_data1.sbbh} 
db_data1.tpmc1=table.concat(tpmc,"-") 
db_data1.id = tostring(resty_uuid.gen20())
db_data1.server_ip = tb_queue.server_ip
db_data1.sbip = nil
db_data1.zb = nil
db_data1.time = nil
db_data1.st_res = nil
db_data1.gfile2=nil
db_data1.hpys = tb_hpys[tonumber(db_data1.hpys)] or 4
if tonumber(db_data1.hpys) == 1 then    ---黄牌车,大型汽车
    db_data1.hpzl = "1"
elseif tonumber(db_data1.hpys) == 0 then
    db_data1.hpzl = "2" ---蓝色车牌，小型车
elseif tonumber(db_data1.hpys) == 2 then
    db_data1.hpzl = "23"  ---白色车牌，临时入境车
else
    db_data1.hpzl = "99" ---其他
end

if string.find(db_data1.hphm,"鄂A") then  ---本地车
    db_data1.clpz = "nat"    
else
    if db_data1.hphm ~= "无车牌" then          ---外地车
        db_data1.clpz = "wai"
    
    else
       db_data1.clpz="other"  ---无车牌的车
    end
end
---------------------------------------
local list=tb_queue.q_name 
local time1=string.sub(db_data1.time1,1,6)
local mongo_table_name = "gcxx_" ..time1
local mongo_table_namewz = "wzgcxx"
local numlist = ""
local mongo_table_name1= ""

if db_data1.wfxwdm~="00000" then
    mongo_table_name1=mongo_table_namewz
    list="wzlist"
    numlist="gc_wznumby"
else 
    mongo_table_name1=mongo_table_name
    list=tb_queue.q_name  
end   
--------------存mongodb-------------------------------------


local n,err = mongo_method.save_data(mongo_table_name,db_data1)
if not n then 
    utils.gcs_log("error",vssid .."存储数据库出错" ..err)
    ngx.exit(200)
end
------------------插入队列-----------------------------------------
--[[local time2=socket.gettime()*1000
utils.gcs_log("error",vssid .. "time2" ..time2)
local dictime=time2-time1
utils.gcs_log("error",vssid .. "s时间差" ..dictime)--]]
utils.gcs_log("debug",vssid .. "进入存储流程")
local ins_data={}
ins_data[db_data.id]=cjson.encode(db_data)

local res,err=red_method.set_data(ins_data)
err_utils.check_res(tb_queue,model,vssid,err,err_no,nil)

local res,err,err_no=red_method.push("l",list,db_data1.id)
err_utils.check_res(tb_queue,model,vssid,err,err_no,nil)

if  numlist=="gc_wznumby"  then
    up_utils.ins_numbytime(numlist .."hour_" ,hour)--按小时计算违章过车数量
    up_utils.ins_numbytime(numlist .."day_" ,day)--按天数计算违章过车数量
    up_utils.ins_numbytime(numlist .."mon_" ,mon)--按月份计算违章过车数量
    up_utils.ins_numbytime(db_data1.lkbh .."_" ..numlist .."hour_",hour) --按小时计算路口违章过车数量
end
--计算流量
up_utils.ins_numbytime(db_data1.lkbh .."_gc_" ..db_data1.xsfx .."numbyhour_",hour) --路口过车数量
up_utils.ins_numbytime(db_data1.lkbh .."_gc_" ..db_data1.hpzl .."car" ..db_data1.xsfx .."_numbyhour_" ,hour) --按小时计算路口大型车，小型车数量
up_utils.ins_numbytime(db_data1.lkbh .."_gc_" ..db_data1.clpz .."car" ..db_data1.xsfx .."_numbyhour_" ,hour)--按小时路口外地车，本地车数量
up_utils.ins_numbytime("gc_numbyhour_" ,hour)--按小时计算过车流量
up_utils.ins_numbytime("gc_numbyday_",day) --按天数计算过车流量
up_utils.ins_numbytime("gc_numbymon_" ,mon)--按月份计算过车流量

----------------------------流量统计------------------------------






--------------最后一条记录------------------------------------
local key = db_data1.ip.."_enddata"
local ins_data = {}
ins_data[key] = cjson.encode(db_data1)
red_method.set_data(ins_data)

utils.gcs_log("info",vssid .."存储数据库成功" )
